﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.SessionState;
using System.Web;
using System.Data;

namespace AjaxLibrary
{
    class buyerFixedPricePeriod : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler 成员

        bool IHttpHandler.IsReusable
        {
            get { return true; }
        }
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            string param = context.Request.QueryString["Param"];
            switch (param.ToLower())
            {
                case "ifexist":
                    ifExistByBuyerIdAndTime(context.Request, context.Response);
                    break;
                default:
                    context.Response.Output.Write("Unknow request URL---");
                    break;
            }
        }
        private void ifExistByBuyerIdAndTime(HttpRequest httpRequest, HttpResponse httpResponse)
        {
            int buyerId = 0;
            int.TryParse(httpRequest.QueryString["buyerId"], out buyerId);
            DateTime startTime = Convert.ToDateTime(httpRequest.QueryString["startTime"]);
            DateTime endTime = Convert.ToDateTime(httpRequest.QueryString["endTime"]);
            if (buyerId != 0 && startTime!=DateTime.MinValue && endTime!=DateTime.MinValue)
            {
                StringBuilder jsonBuilder = new StringBuilder("[");
                if (logic.buyerFixedPricePeriod.ifExistByBuyerIdAndTime(buyerId, startTime, endTime))
                {
                    DataRow dr = logic.buyerFixedPricePeriod.getLastperiodBybuyerid(buyerId);
                    jsonBuilder.Append("{\"startTime\":\"" + dr["startTime"] + "\",\"endTime\":\"" + dr["endTime"] + "\"}");
                }
                jsonBuilder.Append("]");
                httpResponse.Write(jsonBuilder.ToString());
            }
        }
        #endregion
    }
}
